<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Input;
use App\Admin\Auth;
class AuthController extends Controller
{
    //展示权限列表
    //请求的时候有两次get请求，一次是请求网页资源，第二次请求是page=*&limit=*请求的是列表的数据
    public function lst(Request $request){
            if($request->Input('page')){
                // $data=Auth::get()->toArray();

                $pageNum=$request->input('page');
                $limit=$request->input('limit');
                $count = Auth::count();
                $msg="";
                $code=0;
                // $data = Manager::paginate($nums)->toArray();
                //$data=Manager::get()->toArray();
                //dd($limit);
                $page=$pageNum-1;
                if ($page != 0) {
                    $page = $limit * $page;
                    //$limit=$limit*$pageNum;
                }
                //$data = Manager::offset($page)->limit($limit)->get()->toArray();

                $data = Auth::offset($page)->limit($limit)->get();



                foreach($data as $val){
                    if($val->Auth){
                        $val->pid=$val->Auth->auth_name;
                    }else {
                        $val->pid=$val->controller=$val->action='N/A';
                    }
                }
                // dd(compact('data','count','msg','code'));
                return (compact('data','count','msg','code'));
            }else return view('admin.auth.lst');


    }

    public function add(Request $request){
        if($request->isMethod('post')){
            $data=$request->Input('add_data');
            //$data=$request->except(['_token']);
           // dd($data);
            $res=Auth::insert($data);

            if($res)$status='ok';
            else $status='error';
            return (compact('status'));
        }else{
            //查询父级权限并在视图中展出
            $parents=Auth::where('pid','=','0')->get();
            //dd($parents);
            return view('admin.auth.add',compact('parents'));
        }

    }


    public function del(Request $request)
    {
      //d($request->input('del_id'));
       // $status='d';
      // dd($request->input('del_num'));

      /* Model的delete()方法返回bool类型，destory()返回影响的行数 */
       if($request->input('del_num')=='one')
       {
            $id=$request->input('del_id');
            $result=Auth::where('id',$id)->delete();

            // dd($result);
            if($result)$status='ok';
            else $status='error';
            return (compact('status'));
       }
       elseif($request->input('del_num')=='several')
       {
          //  dd($request->input('del_id'));
            $ids=array_values($request->input('del_id'));
            $nums=Auth::destroy($ids);
            if($nums)$status='ok';
            else $status='error';
            return (compact('status','nums'));

       }
    }

    public function edit(Request $request,$id){
        $data=Auth::where('id',$id)->get();
//        dd($data);

    }

    public function snake(){
        return view('admin.auth.snake');
    }
}
